/*********************************************************************************
 *      Copyright:  (C) 2024 linuxer<linuxer@email.com>
 *                  All rights reserved.
 *
 *       Filename:  packet.c
 *    Description:  This file
 *
 *        Version:  1.0.0(01/04/24)
 *         Author:  linuxer <linuxer@email.com>
 *      ChangeLog:  1, Release initial version on "01/04/24 13:59:08"
 *
 ********************************************************************************/



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <stdbool.h>
#include "cJSON.h"
#include <time.h>
#include "temperature.h"
#include "iniparser.h"
#include "packet.h"
#include "t_time.h"
#include "humidity.h"
#define TIME_LEN 32

int get_mqttdata(char *ini_path, mqtt_report_t *mqtt, int option)
{
    char            *con_plat;
    char            *send_plat;
    int             n;
    dictionary      *ini;

    if( !ini_path || !mqtt )
    {
        printf("error\n");
        return -1;
    }

    ini = iniparser_load(ini_path);
    if( !ini )
    {
        printf("parsing failure!\n");
        return -2;
    }

    //Check if the ini file contains a section
    n = iniparser_getnsec(ini);
    if(n<=0)
    {
        iniparser_freedict(ini);
        return -3;
    }

    if( option == 1 )//aliyun
    {
        con_plat = "ali_connect";
        send_plat = "ali_send";

        if(parser_config(ini, &mqtt, con_plat, send_plat)<0)
        {
            iniparser_freedict(ini);
            return -4; // aliyun配置读取失败
        }
    }
    else if( option == 2 )//tenxunyun
    {
        con_plat = "tenxun_connect";
        send_plat = "tenxun_send";

        if(parser_config(ini, &mqtt, con_plat, send_plat)<0)
        {
            iniparser_freedict(ini);
            return -4; // aliyun配置读取失败
        }

    }
    else if( option == 3 )//huaweiyun
    {
        con_plat = "huawei_connect";
        send_plat = "huawei_send";

        if(parser_config(ini, &mqtt, con_plat, send_plat)<0)
        {
            iniparser_freedict(ini);
            return -4; // aliyun配置读取失败
        }
    }

    log_error("mqtt.hostname:%s mqtt.topic:%s mqtt.intervel_alive:%d", mqtt->hostname, mqtt->topic, mqtt->keep_alive_interval);
    iniparser_freedict(ini);
}


int parser_config(dictionary *ini, mqtt_report_t **mqtt, char *con_plat, char *send_plat)
{
    char            conf[64];

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:host", con_plat);
    const char      *host = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:port", con_plat);
    int             port = iniparser_getint(ini, conf, 1883);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:username", con_plat);
    const char      *username = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:passwd", con_plat);
    const char      *passwd = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:clientid", con_plat);
    const char      *clientid = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:topic", send_plat);
    const char      *topic = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:method", send_plat);
    const char      *method = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:id", send_plat);
    const char      *jsonid = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "%s:version", send_plat);
    const char      *version = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "common:session");
    bool            session = iniparser_getboolean(ini, conf, true);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "common:keep_alive_interval");
    int             keep_alive_interval = iniparser_getint(ini, conf, 60);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "common:identifier", send_plat);
    const char      *identifier = iniparser_getstring(ini, conf, NULL);

    memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "common:identifier1", send_plat);
    const char      *identifier1 = iniparser_getstring(ini, conf, NULL);
   
	memset(conf, 0, sizeof(conf));
    snprintf(conf, sizeof(conf), "common:Qos", send_plat);
    int             Qos = iniparser_getint(ini, conf,0);


    strncpy((*mqtt)->hostname, host, sizeof((*mqtt)->hostname));
    (*mqtt)->port = port;
    strncpy((*mqtt)->username, username, sizeof((*mqtt)->username));
    strncpy((*mqtt)->passwd, passwd, sizeof((*mqtt)->passwd));
    strncpy((*mqtt)->clientid, clientid, sizeof((*mqtt)->clientid));
    strncpy((*mqtt)->topic, topic, sizeof((*mqtt)->topic));
    strncpy((*mqtt)->method, method, sizeof((*mqtt)->method));
    strncpy((*mqtt)->jsonid, jsonid, sizeof((*mqtt)->jsonid));
    strncpy((*mqtt)->version, version, sizeof((*mqtt)->version));
    (*mqtt)->session = session;
    (*mqtt)->keep_alive_interval = keep_alive_interval;	 
	printf("----\n");
	strncpy((*mqtt)->identifier, identifier, sizeof((*mqtt)->identifier)); 	
	printf("----\n");
	strncpy((*mqtt)->identifier1, identifier1, sizeof((*mqtt)->identifier1));
    (*mqtt)->Qos = Qos;
}

int pack_data(mqtt_report_t *mqtt, char *msg, int size, int option)
{
    char            *msg1 = NULL;
    ReportInfo      report_info;
    time_t          now_time;
    struct tm       tm_ptr;
    char            cur_time[64];

    if( !mqtt || !msg )
    {
        log_error("Parameter error!\n");
        return -1;
    }

    if( get_temperature(&report_info.temperature))
    {
        log_error("Get temperature error:%s\n", strerror(errno));
        return -2;
    }

    if( get_humidity(&report_info.humidity) < 0)
    {
        log_error("Get humidity error:%s\n", strerror(errno));
        return -3;
    }

	printf("get humidity\n");

    memset(cur_time,0,strlen(cur_time));
    get_time(&tm_ptr);
    strftime(cur_time, strlen(cur_time), "%Y-%m-%d %H:%M:%S", &tm_ptr);
    strncpy(report_info.cur_time, cur_time, sizeof(report_info.cur_time));

    cJSON   * root =  cJSON_CreateObject();
    memset(root, 0, sizeof(root));

    if( option == 1)
    {
        pack_json_ali(mqtt, &root, report_info);
    }
    else if( option == 2)
    {
        pack_json_tenxun(mqtt, &root, report_info);
    }
    else if( option == 3)
    {
        pack_json_huawei(mqtt, &root, report_info);
    }

    msg1=cJSON_Print(root);
    memset(msg, 0, strlen(msg));
    strncpy(msg , msg1 , size);
    free(msg1);

    return 0;
}


int pack_json_ali(mqtt_report_t *mqtt, cJSON **root, ReportInfo report_info)
{
    cJSON   * item1 =  cJSON_CreateObject();
    cJSON   * item2 =  cJSON_CreateObject();

    memset(item1, 0, sizeof(item1));
    memset(item2, 0, sizeof(item2));

    cJSON_AddItemToObject(*root, "method", cJSON_CreateString(mqtt->method));
    cJSON_AddItemToObject(*root, "jsonid", cJSON_CreateString(mqtt->jsonid));
    cJSON_AddItemToObject(*root, "params",item1);
    cJSON_AddItemToObject(*root, "time",cJSON_CreateString(report_info.cur_time));
    cJSON_AddItemToObject(item1, mqtt->identifier, cJSON_CreateNumber(report_info.temperature));    
	cJSON_AddItemToObject(*root, "version", cJSON_CreateString(mqtt->version));

}

int pack_json_tenxun(mqtt_report_t *mqtt, cJSON **root, ReportInfo report_info)
{
    cJSON   * item1 =  cJSON_CreateObject();
    cJSON   * item2 =  cJSON_CreateObject();

    memset(item1, 0, sizeof(item1));
    memset(item2, 0, sizeof(item2));

    cJSON_AddItemToObject(*root, "type", cJSON_CreateString(mqtt->method));
    cJSON_AddItemToObject(*root, "state", item1);
    cJSON_AddItemToObject(item1, "reported",item2);
    cJSON_AddItemToObject(item2, mqtt->identifier, cJSON_CreateNumber(report_info.temperature));
    cJSON_AddItemToObject(*root, "version", cJSON_CreateString(mqtt->version));
    cJSON_AddItemToObject(*root, "clientToken", cJSON_CreateString(mqtt->jsonid));

}


int pack_json_huawei(mqtt_report_t *mqtt,cJSON **root, ReportInfo report_info)
{
    cJSON   * item1 =  cJSON_CreateObject();
    cJSON   * item2 =  cJSON_CreateObject();
    cJSON   * item3 =  cJSON_CreateArray();

    memset(item1, 0, sizeof(item1));
    memset(item2, 0, sizeof(item2));
    memset(item3, 0, sizeof(item3));

    cJSON_AddItemToObject(*root, "services",item3);
    cJSON_AddItemToObject(item1, "service_id",cJSON_CreateString(mqtt->method));
    cJSON_AddItemToObject(item1, "properties",item2); 
    cJSON_AddItemToObject(item2, mqtt->identifier , cJSON_CreateNumber(report_info.temperature));
    cJSON_AddItemToObject(item2, mqtt->identifier1 , cJSON_CreateNumber(report_info.humidity));
	cJSON_AddItemToArray(item3, item1);
}


